$(function(){
    //切换登录
    $('#goreg').on('click',function(){
    $('#longinBox').hide()
    $('#regBox').show()
})
    //切换注册
    $('#gologin').on('click',function(){
    $('#regBox').hide()
    $('#longinBox').show()
})

  //正则判断账号密码，再次输入密码是否一致
const form=layui.form
  form.verify({
      username:[
          /^[a-zA-Z0-9]{1,10}$/,
          '用户名必须1到10位数字和字母'
      ],
      password:[
          /^[\S]{6,12}$/,
          '密码必须是6到12位数字和字母'
      ],

      rePwd:function(value){
          let pwd=$('#regBox [name="password"]').val()
          if(value!=pwd){
              return '两次输入的密码不一致!'
          }
      }
  })
  
  let layer=layui.layer
  //注册功能
  $('#formReg').on('submit',function(e){
      e.preventDefault()
      axios({
          url:'/api/reguser',
          method:'post',
          data:$(this).serialize()
      }).then(({data:res})=>{
         if(res.status!==0){
           return layer.msg(res.message)
         }

         layer.msg('恭喜您'+res.message)
          $('#formReg')[0].reset()
          $('#gologin').click()
      });
  })
  //登录功能
  $('#formLogin').on('submit',function(e){
     e.preventDefault()
     axios({
         url:'/api/login',
         method:'post',
         data:$(this).serialize()
     }).then(({data:res})=>{
         if(res.status!==0){
           return layer.msg(res.message)
         }
         layer.msg('恭喜您'+res.message)
         localStorage.setItem('token',res.token)
          setTimeout(()=>{
             location.href='/index.html'
          },1000)
     })
  })

})